/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame.Model;

import java.util.ArrayList;
import java.util.HashMap;

import mygame.Manager;
import mygame.Backbone.GlobalVars;
import mygame.Object.GameObject;
import mygame.Object.Grabable;
import mygame.Object.Pickable;
import mygame.Scene.PlacementTriangle;

import com.jme3.asset.AssetManager;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;

/**
 *
 * @author Tobi
 */
public class ModelManager {

    private Node ModelEnv;
    private AssetManager asm;
    public static HashMap<Integer, GameObject> InteractiveObjects = new HashMap<Integer, GameObject>();

    public ModelManager(Node root, AssetManager asm) {
        ModelEnv = new Node("Model_Environment");
        root.attachChild(ModelEnv);
        this.asm = asm;
    }
    
    public void init() {    	
        //placeItems();
        //Manager.GetInstance().getRoot().setLocalScale(5f);
    }

    public void placeItems() {
        ArrayList<PlacementTriangle> tl = Manager.GetInstance().getSceneManager().getGroundTriangles();

        boolean loaded = Manager.GetInstance().getSavingHandler().load();
        if (loaded) {
            Manager.GetInstance().getSavingHandler().load_ObjectPlacement();
            Manager.GetInstance().getSavingHandler().load_Inventory();
            //Manager.GetInstance().getSavingHandler().load_PlayerPosition();
        } else {
            //Place Usables
            //Manager.GetInstance().getMyCam().setPlayerPosition((tl.get(0).getPosition()).addLocal(new Vector3f(0f,4f,0f)));
            
            for (int x = 0; x < 2; x++) {
                int y = 0;
                while (tl.get(y = GlobalVars.random.nextInt(tl.size())).hasObject()) {
                }
                new Grabable(GlobalVars.OBJECT_NAME_STRAUCH).placeObject(tl.get(y), y);
            }
            //Place Pickables
            for (int x = 0; x < 2; x++) {
                int y = 0;
                while (tl.get(y = GlobalVars.random.nextInt(tl.size())).hasObject()) {
                }
                new Pickable(GlobalVars.OBJECT_NAME_STONE).placeObject(tl.get(y), y);
            }
            //Place Pickables
            for (int x = 0; x < 1; x++) {
                int y = 0;
                while (tl.get(y = GlobalVars.random.nextInt(tl.size())).hasObject()) {
                }
                new Pickable(GlobalVars.OBJECT_NAME_AST).placeObject(tl.get(y), y);
            }
        }
    }

    public Spatial loadObject(String name) {
        Spatial object = asm.loadModel(name);
        attach(object);
        return object;
    }

    public void attach(Spatial child) {
        this.ModelEnv.attachChild(child);
    }

    public void detach(Spatial child) {
        this.ModelEnv.detachChild(child);
    }

    public Node getModelEnvironment() {
        return this.ModelEnv;
    }
}
